#include "TextureAsset.h"

TextureAsset::TextureAsset(const std::string& id, const std::wstring& path)
    : Asset(id, AssetType::Texture), m_path(path), m_image(nullptr)
{
}

TextureAsset::~TextureAsset()
{
    Unload();
}

bool TextureAsset::Load()
{
    if (m_isLoaded)
        return true;

    m_image = new IMAGE();

    // 使用EasyX的loadimage函数加载图像
    loadimage(m_image, m_path.c_str());

    // 通过检查图像的宽度和高度来判断是否加载成功
    if (m_image->getwidth() == 0 || m_image->getheight() == 0)
    {
        delete m_image;
        m_image = nullptr;
        return false;
    }

    m_isLoaded = true;
    return true;
}

void TextureAsset::Unload()
{
    if (m_image)
    {
        delete m_image;
        m_image = nullptr;
    }
    m_isLoaded = false;
}

int TextureAsset::GetWidth() const
{
    return m_image ? m_image->getwidth() : 0;
}

int TextureAsset::GetHeight() const
{
    return m_image ? m_image->getheight() : 0;
}
